package cn.cttic.app.service.impl;

import cn.cttic.order.api.domain.BaseInstallOrder;
import cn.cttic.order.api.domain.bo.BaseInstallOrderBo;
import cn.cttic.order.api.domain.vo.BaseInstallOrderVo;
import cn.hutool.core.bean.BeanUtil;
import cn.cttic.common.core.utils.StringUtils;
import cn.cttic.common.mybatis.core.page.PageQuery;
import cn.cttic.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import cn.cttic.app.mapper.BaseInstallOrderMapper;
import cn.cttic.app.service.IBaseInstallOrderService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 安装订单Service业务层处理
 *
 * @author Times.Studio
 * @date 2024-05-29
 */
@RequiredArgsConstructor
@Service
public class BaseInstallOrderServiceImpl implements IBaseInstallOrderService {

    private final BaseInstallOrderMapper baseMapper;

    /**
     * 查询安装订单
     */
    @Override
    public BaseInstallOrderVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询安装订单列表
     */
    @Override
    public TableDataInfo<BaseInstallOrderVo> queryPageList(BaseInstallOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseInstallOrder> lqw = buildQueryWrapper(bo);
        Page<BaseInstallOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询安装订单列表
     */
    @Override
    public List<BaseInstallOrderVo> queryList(BaseInstallOrderBo bo) {
        LambdaQueryWrapper<BaseInstallOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseInstallOrder> buildQueryWrapper(BaseInstallOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseInstallOrder> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSaleOrderId()), BaseInstallOrder::getSaleOrderId, bo.getSaleOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getCarNo()), BaseInstallOrder::getCarNo, bo.getCarNo());
        lqw.eq(StringUtils.isNotBlank(bo.getInstallCode()), BaseInstallOrder::getInstallCode, bo.getInstallCode());
        lqw.eq(StringUtils.isNotBlank(bo.getInstallUrl()), BaseInstallOrder::getInstallUrl, bo.getInstallUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getInstallManId()), BaseInstallOrder::getInstallManId, bo.getInstallManId());
        lqw.eq(StringUtils.isNotBlank(bo.getNoticeSmsId()), BaseInstallOrder::getNoticeSmsId, bo.getNoticeSmsId());
        lqw.eq(bo.getNoticeStatus() != null, BaseInstallOrder::getNoticeStatus, bo.getNoticeStatus());
        lqw.eq(bo.getStatus() != null, BaseInstallOrder::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增安装订单
     */
    @Override
    public Boolean insertByBo(BaseInstallOrderBo bo) {
        BaseInstallOrder add = BeanUtil.toBean(bo, BaseInstallOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改安装订单
     */
    @Override
    public Boolean updateByBo(BaseInstallOrderBo bo) {
        BaseInstallOrder update = BeanUtil.toBean(bo, BaseInstallOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseInstallOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除安装订单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
